package Homework;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.Socket;
import java.util.Scanner;

//客户端
public class Client {

	@SuppressWarnings("resource")
	public static void main(String[] args) {
		//创建客户端的套接字，输入服务器的接口
		Scanner scanner = new Scanner(System.in);
		Socket socket=null;
		DataInputStream dis=null;
		DataOutputStream dos=null;
		try {
			socket = new Socket("localhost",8876);
			System.out.println("客户端启动了。。。。。。");
			
			//创建输入输出流，输出和读写信息 
			dis = new DataInputStream(socket.getInputStream());
			dos = new DataOutputStream(socket.getOutputStream());
			
			//向服务器输入请求
			System.out.print("请输入用户名：");
			String userName=scanner.next();
			System.out.print("请输入登录密码：");
			String password=scanner.next();
			
			dos.writeUTF("用户名："+userName+"，密码："+password);
			
			//读取服务器回复的信息
			String infoFromServer = dis.readUTF();
			System.out.println("服务器发来了："+infoFromServer);
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			try {
				dis.close();
				dos.close();
				socket.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
}
